package com.xhwl.centiotdata.action.eesd.Impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.action.eesd.IEesdAction;
import com.xhwl.centiotdata.handler.eesd.EesdFactory;
import com.xhwl.centiotdata.handler.eesd.EesdHandler;
import com.xhwl.centiotdata.pojo.dto.eesd.EesdConfigDTO;
import com.xhwl.centiotdata.pojo.enums.device.DeviceProductCategoryEnum;
import com.xhwl.centiotdata.service.integrateconfig.EESDIntegrationConfig;
import com.xhwl.centiotdata.service.integrateconfig.EESDSupplierConfig;
import com.xhwl.common.dto.cent.eesd.RechargeRecordDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author tangwei
 * @description 能耗水电缴费服务实现类
 * @create 2023-05-08 16:22
 */
@Slf4j
@Service
public class IEesdActionImpl implements IEesdAction {

    @Autowired
    EESDIntegrationConfig integrationConfig;
    @Autowired
    EESDSupplierConfig supplierConfig;

    @Override
    public ResultJson rechargeElectricityAndWater(String params) {
        RechargeRecordDTO dto = JSONObject.parseObject(params, RechargeRecordDTO.class);
        Integer projectId = dto.getProjectId();
        Integer productId = dto.getProductId();
        EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
        if (Objects.isNull(handler)) {
            log.error("未找到处理方式，dto:{}", dto);
            return ResultJson.fail("未找到处理方式");
        }
        List<EesdConfigDTO> list = integrationConfig.getIntegrationConfig(productId);
        List<EesdConfigDTO> configDTOList = list.stream().filter(obj -> Objects.equals(projectId, obj.getProjectId())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(configDTOList)) {
            log.error("集成配置为空,list:{}", list);
            return ResultJson.fail("集成配置为空");
        }
        EesdConfigDTO configDTO = configDTOList.get(0);
        return handler.rechargeElectricityAndWater(configDTO, dto);
    }

    @Override
    public ResultJson controlElectricityMeter(JSONObject params) {
        String deviceNo = params.getString("deviceNo");
        Integer cutoffState = params.getInteger("cutoffState");
        Integer productId = params.getInteger("productId");
        EesdHandler handler = EesdFactory.getInvokeStrategy(productId);
        DeviceProjSupplierCategory config = getDeviceProjSupplierCategory(productId);
        if (Objects.isNull(handler)) {
            return ResultJson.fail("未找到处理方式");
        }
        return handler.controlElectricityMeter(config, deviceNo, cutoffState);
    }

    @Override
    public void electricityPayRecordSync(JSONObject params) {
        EesdHandler handler = EesdFactory.getInvokeStrategy(params.getInteger("productId"));
        if (Objects.isNull(handler)) {
            return;
        }
        DeviceProjSupplierCategory config = getDeviceProjSupplierCategory(params.getInteger("productId"));
        if (Objects.isNull(config)) {
            return;
        }
        handler.pullKeDeElectricityPaymentData(config, null, params.getString("deviceNo"));
    }

    @Override
    public void waterPayRecordSync(JSONObject params) {
        EesdHandler handler = EesdFactory.getInvokeStrategy(params.getInteger("productId"));
        if (Objects.isNull(handler)) {
            return;
        }
        DeviceProjSupplierCategory config = getDeviceProjSupplierCategory(params.getInteger("productId"));
        if (Objects.isNull(config)) {
            return;
        }
        handler.pullKeDeWaterPaymentData(config, null, params.getString("deviceNo"));
    }

    @Override
    public void ownAlarmPush(JSONObject params) {
        EesdHandler handler = EesdFactory.getInvokeStrategy(params.getInteger("productId"));
        if (Objects.isNull(handler)) {
            return;
        }
        DeviceProjSupplierCategory config = getDeviceProjSupplierCategory(params.getInteger("productId"));
        if (Objects.isNull(config)) {
            return;
        }
        handler.ownAlarmPush(config, params);
    }

    private DeviceProjSupplierCategory getDeviceProjSupplierCategory(Integer productId) {
        List<DeviceProjSupplierCategory> supplierCategories = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId()).stream()
                .filter(c -> c.getProductId().equals(productId) && !c.getProperties().isEmpty())
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(supplierCategories)) {
            log.error("未获取能耗系统相关供应商,list:{}",supplierCategories);
        }
        return supplierCategories.get(0);
    }

}
